package com.zb.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.entity.ZjStoreOutboundMaterialCode;
import com.zb.mapper.ZjStoreOutboundMaterialCodeMapper;
import com.zb.service.ZjStoreOutboundMaterialCodeService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 出库物料一物一码表 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2024-01-12
 */
@Service
public class ZjStoreOutboundMaterialCodeServiceImpl extends ServiceImpl<ZjStoreOutboundMaterialCodeMapper, ZjStoreOutboundMaterialCode> implements ZjStoreOutboundMaterialCodeService {

    // 保存出库物料一物一码
    @Override
    public void save(Long outboundId, List<ZjStoreOutboundMaterialCode> addOutboundMaterialCodes) {
        if (CollectionUtil.isEmpty(addOutboundMaterialCodes)) {
            return;
        }
        deleteByStoreOutboundId(outboundId);
        this.saveBatch(addOutboundMaterialCodes);
    }

    // 根据出库单ID获取出库物料一物一码
    @Override
    public List<ZjStoreOutboundMaterialCode> getbyStoreOutboundId(Long storeOutboundId) {
        QueryWrapper<ZjStoreOutboundMaterialCode> wrapper = new QueryWrapper<>();
        wrapper.select("id", "store_outbound_id", "store_outbound_material_id", "type", "code_id", "code");
        wrapper.eq("store_outbound_id", storeOutboundId);
        return this.list(wrapper);
    }

    // 根据出库单ID删除
    @Override
    public void deleteByStoreOutboundId(Long storeOutboundId) {
        QueryWrapper<ZjStoreOutboundMaterialCode> wrapper = new QueryWrapper<>();
        wrapper.eq("store_outbound_id", storeOutboundId);
        this.remove(wrapper);
    }

    // 根据一物一码删除
    @Override
    public void deletebyCodeIds(List<Long> codeIds) {
        if (CollectionUtil.isEmpty(codeIds)) {
            return;
        }
        QueryWrapper<ZjStoreOutboundMaterialCode> wrapper = new QueryWrapper<>();
        wrapper.in("code_id", codeIds);
        this.remove(wrapper);
    }

    // 根据删除的一物一码ID获取关联出库商品的一物一码
    @Override
    public List<String> getCodeByDelCodeIds(List<Long> delCodeIds) {
        if (CollectionUtil.isEmpty(delCodeIds)) {
            return new ArrayList<>();
        }
        QueryWrapper<ZjStoreOutboundMaterialCode> wrapper = new QueryWrapper<>();
        wrapper.last("limit 10")
                .select("code")
                .in("code_id", delCodeIds);
        return this.list(wrapper).stream().map(ZjStoreOutboundMaterialCode::getCode).distinct().toList();
    }
}
